import Vue from 'vue'
import VueRouter from 'vue-router'

import index from '../views/home.vue'


import Login from "../views/login/login"


import yhhuvhxs from '../views/yhhu/yhhuvhxs'
import cockpit from '../views/cockpit/index'
import taixin from '../views/taixin/taixin'


import store from '../store'
import { Message } from 'element-ui';
Vue.use(VueRouter)

const originalPush = VueRouter.prototype.push

VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}
const routes = [
  {
    path: '/',
    redirect: '/login',
  },
  {
    path: '/files',
    name: 'files',
    component: () => import('@/views/details/files')
  },
  {
    path: '/login',
    name: 'login',
    component: Login
  },
  {
    path: '/manage',
    name: 'manage',
    component: () => import('@/views/manage.vue'),
    children: [
      {
        path: '/systemgl/systemglzuzhi',
        name: 'systemglzuzhi',
        // meta: {
        //   keepAlive: true  // 需要缓存
        // },
        component: () => import('@/views/systemgl/systemglzuzhi')
      },
      {
        path: '/systemgl/systemglhou',
        name: 'systemglhou',
        // meta: {
        //   keepAlive: true  // 需要缓存
        // },
        component: () => import('@/views/systemgl/systemglhou')
      },
      {
        path: '/systemgl/systemgljuese',
        name: 'systemgljuese',
        // meta: {
        //   keepAlive: true  // 需要缓存
        // },
        component: () => import('@/views/systemgl/systemgljuese')
      },
      {
        path: '/systemgl/systemgljueseadd',
        name: 'systemgljueseadd',
        component: () => import('@/views/systemgl/systemgljueseadd')
      },
      // {
      //   path: '/systemgl/systemglzuzhibianj',
      //   name: 'systemglzuzhibianj',
      //   component: () => import('@/views/systemgl/systemglzuzhibianj')
      // },
      {
        path: '/systemgl/systemglzuzhibianj2',
        name: 'systemglzuzhibianj2',
        component: () => import('@/views/systemgl/systemglzuzhibianj2')
      },
      {
        path: '/systemgl/systemglshuju',
        name: 'systemglshuju',
        // meta: {
        //   keepAlive: true  // 需要缓存
        // },
        component: () => import('@/views/systemgl/systemglshuju')
      },
      {
        path: '/systemgl/feedback',
        name: 'feedback',
        // meta: {
        //   keepAlive: true  // 需要缓存
        // },
        component: () => import('@/views/systemgl/feedback')
      },
    ]
  },
  {
    path: '/editdata',
    name: 'editData',
    component: () => import('@/views/systemgl/dataPage/edit.vue'),
  },
  {
    path: "/home",
    name: 'index',
    component: index,
    children: [
      // 载体资源库
      {
        path: '/cockpit',
        name: 'cockpit',
        meta: {
          keepAlive: true  // 需要缓存
        },
        component: cockpit,
      },
      {
        path: '/atlas',
        name: 'atlas',
        meta: {
          keepAlive: true  // 需要缓存
        },
        component: () => import('@/views/atlas/atlas')
      },

      {
        path: '/radar',
        name: 'radar',
        meta: {
          keepAlive: true  // 需要缓存
        },
        component: () => import('@/views/radar/radar')
      },
      {
        path: '/map',
        name: 'map',
        meta: {
          keepAlive: true  // 需要缓存
        },
        component: () => import('@/views/map/map')
      },
      {
        path: '/portrait',
        name: 'portrait',
        meta: {
          keepAlive: true  // 需要缓存
        },
        component: () => import('@/views/portrait/portrait')
      },
      {
        path: '/resources',
        name: 'resources',
        // meta: {
        //   keepAlive: true  // 需要缓存
        // },
        component: () => import('@/views/resources/resources'),
        children: [{
          path: "/industrymap",
          name: 'industrymap',
          // meta: {
          //   keepAlive: true  // 需要缓存
          // },
          component: () => import('@/views/resources/pages/map'),
        },
        {
          path: "/filling",
          name: 'filling',
          // meta: {
          //   keepAlive: true  // 需要缓存
          // },
          component: () => import('@/views/resources/pages/filling'),
        },
        {
          path: "/estimate",
          name: 'estimate',
          meta: {
            keepAlive: true  // 需要缓存
          },
          component: () => import('@/views/resources/pages/estimate'),
        }]
      },
      {
        path: '/yhhuvhxs',
        name: 'yhhuvhxs',
        meta: {
          keepAlive: true  // 需要缓存
        },
        component: yhhuvhxs
      },
      {
        path: '/taixin',
        name: 'taixin',
        component: taixin
      },
      {
        path: '/projectgl',
        name: 'projectgl',
        meta: {
          keepAlive: true  // 需要缓存
        },
        component: () => import('@/views/project/project'),
        children: [
          {
            path: "/overview",
            name: 'overview',
            // meta: {
            //   keepAlive: true  // 需要缓存
            // },
            component: () => import('@/views/project/pages/overview'),
          }, {
            path: "/myproject",
            name: 'myproject',
            // meta: {
            //   keepAlive: true  // 需要缓存
            // },
            component: () => import('@/views/project/pages/myproject'),
          },
          {
            path: "/infoaccountbook",
            name: 'infoaccountbook',
            // meta: {
            //   keepAlive: true  // 需要缓存
            // },
            component: () => import('@/views/project/pages/infoaccountbook'),
          },
          {
            path: "/projectaccountbook",
            name: 'projectaccountbook',
            // meta: {
            //   keepAlive: true  // 需要缓存
            // },
            component: () => import('@/views/project/pages/projectaccountbook'),
          },
          {
            path: "/statistics",
            name: 'statistics',
            // meta: {
            //   keepAlive: true  // 需要缓存
            // },
            component: () => import('@/views/project/pages/statistics'),
          },
          {
            path: "/ranking",
            name: 'ranking',
            // meta: {
            //   keepAlive: true  // 需要缓存
            // },
            component: () => import('@/views/project/pages/ranking'),
          },
          {
            path: "/score",
            name: 'score',
            // meta: {
            //   keepAlive: true  // 需要缓存
            // },
            component: () => import('@/views/project/pages/score'),
          },
        ]
      },
      {
        path: '/projectmanage',
        name: 'projectmanage',
        component: () => import('@/views/project_three/project'),
        children: [
          {
            path: "/overview3",
            name: 'overview3',
            component: () => import('@/views/project_three/pages/overview.vue'),
          },
          {
            path: "/projectPush4",
            name: 'projectPush4',
            component: () => import('@/views/project_three/pages/projectPush.vue'),
          },
          {
            path: "/keyProject4",
            name: 'keyProject4',
            // meta: {
            //   keepAlive: true  // 需要缓存
            // },
            component: () => import('@/views/project_three/pages/keyProject.vue'),
          },
          {
            path: "/ledger4",
            name: 'ledger4',
            redirect: '/ledger1',
            meta: {
              keepAlive: true  // 需要缓存
            },
          },
          {
            path: "/ledger1",
            name: 'ledger1',
            redirect: '',
            // meta: {
            //   keepAlive: true  // 需要缓存
            // },
            component: () => import('@/views/project_three/pages/ledgerTalk.vue'),
          },
          {
            path: "/ledger2",
            name: 'ledger2',
            redirect: '',
            // meta: {
            //   keepAlive: true  // 需要缓存
            // },
            component: () => import('@/views/project_three/pages/ledgerSign.vue'),
          },
          {
            path: "/ledger3",
            name: 'ledger3',
            redirect: '',
            // meta: {
            //   keepAlive: true  // 需要缓存
            // },
            component: () => import('@/views/project_three/pages/ledgerStart.vue'),
          },
          {
            path: "/analysis",
            name: 'analysis',
            // meta: {
            //   keepAlive: true  // 需要缓存
            // },
            redirect: '/indexAnalysis',
            // component: () => import('@/views/project_three/pages/statistics.vue'),
          },
          {
            path: "/indexAnalysis",
            name: 'indexAnalysis',
            meta: {
              keepAlive: true  // 需要缓存
            },
            component: () => import('@/views/project_three/pages/indexAnalysis.vue'),
          },
          {
            path: "/projectAnalysis",
            name: 'projectAnalysis',
            meta: {
              keepAlive: true  // 需要缓存
            },
            component: () => import('@/views/project_three/pages/projectAnalysis.vue'),
          },
          {
            path: "/myproject3",
            name: 'myproject3',
            meta: {
              keepAlive: true  // 需要缓存
            },
            component: () => import('@/views/project_three/pages/myproject'),
          },
          {
            path: "/account3",
            name: 'account3',
            meta: {
              keepAlive: true  // 需要缓存
            },
            component: () => import('@/views/project_three/pages/account'),
          },

          {
            path: "/statistics3",
            name: 'statistics3',
            meta: {
              keepAlive: true  // 需要缓存
            },
            component: () => import('@/views/project_three/pages/statistics'),
          },
        ]
      },
      // 列表页
      {
        path: '/radareplist',
        name: 'radareplist',
        meta: {
          keepAlive: true  // 需要缓存
        }, component: () => import('@/views/list/radarEpList')
      },
      {
        path: '/eplist/:activeMenu/:district/:type',
        name: 'eplist',
        meta: {
          keepAlive: true  // 需要缓存
        },
        component: () => import('@/views/list/eplist')
      },
      {
        path: '/platformlist/:activeMenu/:district/:type',
        name: 'platform',
        meta: {
          keepAlive: true  // 需要缓存
        }, component: () => import('@/views/list/platformlist')
      },
      {
        path: '/schoollist/:activeMenu/:district/:type',
        name: 'schoolList',
        meta: {
          keepAlive: true  // 需要缓存
        }, component: () => import('@/views/list/schoollist')
      },
      // 详情页
      {
        path: '/:activeMenu/park/:id',
        name: 'park',
        component: () => import('@/views/details/park')
      },
      {
        path: '/:activeMenu/keyproject/:id',
        name: 'keyproject',
        component: () => import('@/views/details/keyproject')
      },
      {
        path: '/:activeMenu/policy/:id',
        name: 'policy',
        component: () => import('@/views/details/policy')
      },
      {
        path: '/:activeMenu/enterprise/:type/:id',
        name: 'enterprise',
        component: () => import('@/views/details/enterprise')
      },
      {
        path: '/:activeMenu/institution/:type/:id',
        name: 'institution',
        component: () => import('@/views/details/institution')
      },
      {
        path: '/:activeMenu/school/:type/:id',
        name: 'school',
        component: () => import('@/views/details/school')
      },
      // 载体库 项目选址 四详情页
      {
        path: '/land/:id',
        name: 'land',
        component: () => import('@/views/details/land')
      },
      {
        path: '/workshop/:id',
        name: 'workshop',
        component: () => import('@/views/details/workshop')
      },
      {
        path: '/building/:id',
        name: 'building',
        component: () => import('@/views/details/building')
      },
      {
        path: '/district/:id',
        name: 'district',
        component: () => import('@/views/details/district')
      },
      // 项目详情 --- 编辑  -- 查看
      {
        // :activeMenu/:type/
        path: '/archives/:id',
        name: 'archives',
        // meta: {
        //   keepAlive: true  // 需要缓存
        // },
        component: () => import('@/views/details/archives')
      },
      // 项目详情 --- 编辑  -- 查看
      {
        path: '/project/:activeMenu/:type/:id',
        name: 'project',
        // meta: {
        //   keepAlive: true  // 需要缓存
        // },
        component: () => import('@/views/details/project')
      },
      {
        path: '/sclass/:id',
        name: 'sclass',
        component: () => import('@/views/details/project/sclass')
      },
      {
        path: '/pinfo/:type/:status/:id',
        name: 'pinfo',
        component: () => import('@/views/details/project/info')
      },
      {
        path: '/look/:type/:status/:id',
        name: 'look',
        component: () => import('@/views/details/project/look')
      },
      {
        path: '/examine/:type/:status/:id',
        name: 'examine',
        component: () => import('@/views/details/project/examine')
      },
      {
        path: '/log/:type/:status/:id',
        name: 'log',
        component: () => import('@/views/details/project/log')
      },
      {
        path: '/supervise/:type/:status/:id',
        name: 'supervise',
        component: () => import('@/views/details/project/supervise')
      },
    ]
  }
]

const router = new VueRouter({
  routes,
})
router.beforeEach((to, from, next) => {

  if (to.path === '/login' || to.path === '/' || to.path === '/files') {
    next();
  } else {
    let token = localStorage.getItem('login');
    if (!token) {
      next('/');
    } else {
      let flag = store.state.flag
      let routerList = []
      // 后台管理员
      if (flag == 3) {
        routerList = [
          'editData',
          'systemglzuzhi',
          'systemglhou',
          'systemgljuese',
          'systemgljueseadd',
          'systemglzuzhibianj2',
          'systemglshuju',
          'feedback',
        ]
        if (routerList.includes(to.name)) {
          next()
        } else {
          Message('暂无权限')
        }
      } else if (flag == 1) {  // 游客
        routerList = [
          "index",
          "cockpit",
          "atlas",
          "resources",
          "industrymap",
          "project",
          "policy",
          "keyproject",
          "eplist",
          'school',
          'institution',
          'park',
          'district',
          'land',
          'workshop',
          'building',
          'enterprise',
          "schoolList",
          "platform",
          "estimate",
          "yhhuvhxs",
        ]
        if (to.name == 'resources') {
          router.push({ name: 'industrymap' })
        }
        if (routerList.includes(to.name)) {
          next()
        } else {
          Message('暂无权限')
        }
      } else if (flag == 0) {
        Message('请登录')
        next('/');
      } else if (flag == 2) { //密码登录
        //  1 cockpit
        //  2 atlas
        //  3 radar
        //  4 map
        //  5 portrait
        //  resources
        //   6 industrymap
        //   7  filling
        //   8 estimate
        //  projectgl
        //   9 overview
        //   10 myproject
        //   11 infoaccountbook
        //   12 projectaccountbook
        //   13 statistics
        //   14 ranking
        //   15 score

        let resourcesIndex = localStorage.getItem("resourcesIndex");
        let projectglIndex = localStorage.getItem("projectglIndex");
        let projectThreeIndex = localStorage.getItem("projectThreeIndex");
        let partNoAccessRouter = localStorage.getItem("noAccessRouter").split(',');
        let noAccessRouter = [
          'editData',
          'manage',
          'systemglzuzhi',
          'systemglhou',
          'systemgljuese',
          'systemgljueseadd',
          'systemglzuzhibianj2',
          'systemglshuju',
          'feedback',
          ...partNoAccessRouter
        ]
        if (to.name == "resources") {
          router.push({ name: resourcesIndex })
        }
        if (to.name == "projectgl") {
          router.push({ name: projectglIndex })
        }
        if (to.name == "projectmanage") {
          router.push({ name: projectThreeIndex })
        }
        if (noAccessRouter.includes(to.name)) {
          Message('暂无权限')
        } else {
          next()
        }
      } else if (flag === 5) {
        next()
      }
    }
  }
});
export default router